<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品ID" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入商品ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单ID" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评分(1-5分)" prop="rating">
        <el-select v-model="queryParams.rating" placeholder="请选择评分(1-5分)" clearable>
          <el-option
            v-for="dict in dict.type.t_rating"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="评价标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入评价标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否匿名评价" prop="isAnonymous">
        <el-select v-model="queryParams.isAnonymous" placeholder="请选择是否匿名评价" clearable>
          <el-option
            v-for="dict in dict.type.t_is_anonymous"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="评价状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择评价状态" clearable>
          <el-option
            v-for="dict in dict.type.t_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="有用点赞数" prop="likeCount">
        <el-input
          v-model="queryParams.likeCount"
          placeholder="请输入有用点赞数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:reviews:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:reviews:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:reviews:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:reviews:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reviewsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection"align="center" />
      <el-table-column label="评价ID" align="center" prop="id" />
      <el-table-column label="商品ID" align="center" prop="productId" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="订单ID" align="center" prop="orderId" />
      <el-table-column label="评分(1-5分)" align="center" prop="rating">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.t_rating" :value="scope.row.rating"/>
        </template>
      </el-table-column>
      <el-table-column label="评价标题" align="center" prop="title" />
      <el-table-column label="评价内容" align="center" prop="content"   width="260" />
      <el-table-column label="评价图片，多个图片用逗号分隔" align="center" prop="images" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.images" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="是否匿名评价" align="center" prop="isAnonymous">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.t_is_anonymous" :value="scope.row.isAnonymous"/>
        </template>
      </el-table-column>
      <el-table-column label="评价状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.t_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="有用点赞数" align="center" prop="likeCount" />
      <el-table-column label="商家回复内容" align="center" prop="replyContent" />
      <el-table-column label="商家回复时间" align="center" prop="replyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.replyTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-chat-dot-square" v-if="scope.row.replyContent==null" @click="handleReply(scope.row)">回复</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:reviews:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:reviews:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品评价对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品ID" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入商品ID" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="评分(1-5分)" prop="rating">
          <el-radio-group v-model="form.rating">
            <el-radio
              v-for="dict in dict.type.t_rating"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="评价标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入评价标题" />
        </el-form-item>
        <el-form-item label="评价内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="评价图片，多个图片用逗号分隔" prop="images">
          <image-upload v-model="form.images"/>
        </el-form-item>
        <el-form-item label="是否匿名评价" prop="isAnonymous">
          <el-radio-group v-model="form.isAnonymous">
            <el-radio
              v-for="dict in dict.type.t_is_anonymous"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="评价状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.t_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="有用点赞数" prop="likeCount">
          <el-input v-model="form.likeCount" placeholder="请输入有用点赞数" />
        </el-form-item>
        <el-form-item label="商家回复内容">
          <editor v-model="form.replyContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="商家回复时间" prop="replyTime">
          <el-date-picker clearable
            v-model="form.replyTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择商家回复时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



      <!-- 回复 -->
      <el-dialog title="回复评论" :visible.sync="openReply" width="900px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
              <el-form-item label="评分(1-5分)" prop="rating">
                  <el-radio-group v-model="form.rating" :disabled="true">
                      <el-radio
                              v-for="dict in dict.type.t_rating"
                              :key="dict.value"
                              :label="parseInt(dict.value)"
                      >{{dict.label}}</el-radio>
                  </el-radio-group>
              </el-form-item>
              <el-form-item label="评价标题" prop="title">
                  <el-input v-model="form.title"  placeholder="请输入评价标题"  :readOnly="true" />
              </el-form-item>
              <el-form-item label="评价内容">
                  <editor v-model="form.content" :min-height="192"  :readOnly="true"/>
              </el-form-item>
              <el-form-item label="评价图片，多个图片用逗号分隔" prop="images">
                  <image-upload v-model="form.images" :disabled="true" />
              </el-form-item>


              <el-form-item label="商家回复内容">
                  <editor v-model="form.replyContent" :min-height="192"/>
              </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitReplyForm">确 定</el-button>
<!--              <el-button @click="cancel">取 消</el-button>-->
          </div>
      </el-dialog>


  </div>
</template>

<script>
import { listReviews, getReviews, delReviews, addReviews, updateReviews } from "@/api/system/reviews"

export default {
  name: "Reviews",
  dicts: ['t_rating', 't_is_anonymous', 't_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品评价表格数据
      reviewsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 控制回复弹窗
      openReply:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productId: null,
        orderId: null,
        rating: null,
        title: null,
        content: null,
        isAnonymous: null,
        status: null,
        likeCount: null,
        replyContent: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productId: [
          { required: true, message: "商品ID不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        rating: [
          { required: true, message: "评分(1-5分)不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "评价时间不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询商品评价列表 */
    getList() {
      this.loading = true
      listReviews(this.queryParams).then(response => {
        this.reviewsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productId: null,
        userId: null,
        orderId: null,
        rating: null,
        title: null,
        content: null,
        images: null,
        isAnonymous: null,
        status: null,
        likeCount: null,
        replyContent: null,
        replyTime: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加商品评价"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getReviews(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改商品评价"
      })
    },
      /** 回复按钮操作 */
      handleReply(row) {
          this.reset()
          const id = row.id
          getReviews(id).then(response => {
              this.form = response.data
              this.openReply = true // 打开回复窗口
          })
      },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReviews(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addReviews(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },

      formatDate(date) {


          return `${y}-${m}-${d} ${h}:${i}:${s}`;
      },

      /** 提交回复按钮 */
      submitReplyForm() {
          // 判断回复内容是不是空
          if (!this.form.replyContent) {
              this.$modal.msgError("回复内容不能空，请重新回复！！！")
              this.openReply = false // 设置关闭弹窗
          } else {
              // 回复内容不是空时
              const date = new Date();
              const y = String(date.getFullYear()).padStart(4, '0');// 获取年
              const m = String(date.getMonth() + 1).padStart(2, '0'); // 获取月
              const d = String(date.getDate()).padStart(2, '0'); // 获取时
              const h = String(date.getHours()).padStart(2, '0'); // 获取时
              const i = String(date.getMinutes()).padStart(2, '0');// 获取分
              const s = String(date.getSeconds()).padStart(2, '0');// 获取秒
              const time = `${y}-${m}-${d} ${h}:${i}:${s}`; // 时分秒相加拼接成yyyy-mm-dd HH:mm:ss 如2025-08-18 18:28:08
              this.form.replyTime = time; // 设置回复时的时间是当前时间
              // 直接调用修改接口修改
              updateReviews(this.form).then(response => {
                  this.$modal.msgSuccess("回复成功")
                  this.openReply = false // 设置关闭弹窗
                  this.getList()  // 重新查询列表
              })
          }
      },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除商品评价编号为"' + ids + '"的数据项？').then(function() {
        return delReviews(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/reviews/export', {
        ...this.queryParams
      }, `reviews_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
